package com.wjs.blog.service.Imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wjs.blog.dao.mapper.TagMapper;
import com.wjs.blog.dao.pojo.Tag;
import com.wjs.blog.service.TagService;
import com.wjs.blog.vo.Result;
import com.wjs.blog.vo.TagVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class TagServiceImp implements TagService {
    public TagVo copy(Tag tag) {
        TagVo tagVo = new TagVo();
        BeanUtils.copyProperties(tag, tagVo);
        tagVo.setId(String.valueOf(tag.getId()));
        return tagVo;
    }

    public List<TagVo> copyList(List<Tag> tagList) {
        List<TagVo> tagVoList = new ArrayList<>();
        for (Tag tag : tagList) {
            tagVoList.add(copy(tag));
        }
        return tagVoList;
    }

    @Autowired
    TagMapper tagMapper;

    @Override
    public List<TagVo> findTagsByArticleId(long articleid) {
        List<Tag> tags = tagMapper.findTagsByArticleId(articleid);
        return copyList(tags);
    }

    @Override
    public Result hot(int limit) {
        List<Long> tagid = tagMapper.findHotTagId(limit);
        if (CollectionUtils.isEmpty(tagid)) {
            return Result.success(Collections.emptyList());
        }
        List<Tag> tags = tagMapper.findTagsByTagid(tagid);
        return Result.success(tags);
    }

    @Override
    public Result findAll() {
        List<Tag> tags = tagMapper.selectList(new LambdaQueryWrapper<>());
        return Result.success(copyList(tags));
    }

    @Override
    public Result findAllDetail() {
        List<Tag> tags = tagMapper.selectList(new LambdaQueryWrapper<>());
        return Result.success(copyList(tags));
    }

    @Override
    public Result TagDetailByid(long id) {
        Tag tag = tagMapper.selectById(id);
        return Result.success(copy(tag));
    }
}
